package com.coollang.smartbra.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/10/19 0019.
 */

public class PackageUtils {

    public static String getAppPKName() {
        try {
            return UIUtils.getContext().getPackageName();
        } catch (Exception e) {
        }
        return null;
    }

    public static String getAppVersionName() {
        String versionName = "1.0.0";
        try {
            String pkName = UIUtils.getContext().getPackageName();
            versionName = UIUtils.getContext().getPackageManager().getPackageInfo(pkName, 0).versionName;
            return versionName;
        } catch (Exception e) {
        }
        return versionName;
    }

    public static int getAppVersionCode() {
        try {
            String pkName = UIUtils.getContext().getPackageName();
            int versionCode = UIUtils.getContext().getPackageManager().getPackageInfo(pkName, 0).versionCode;
            return versionCode;
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * SDK版本号
     *
     * @return
     */
    public static int getSDKInt() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * 系统版本号   5.5.1
     *
     * @return
     */
    public static String getSystemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    /**
     * 手机型号
     *
     * @return
     */
    public static String getMODEL() {
        return android.os.Build.MODEL;
    }

    /**
     * @param version    固件版本
     * @param getVersion 服务器固件版本
     * @return 是否需要升级
     * <p>
     * 比较固件版本判断是否需要升级
     * @author han
     */
    public static boolean compareVersion(String version, String getVersion) {

        if (version == null || getVersion == null || "".equalsIgnoreCase(version) || "".equalsIgnoreCase(getVersion)) {
            return false;
        } else {
            String regEx = "[^0-9]";
            version = version.trim();
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(version);
            version = m.replaceAll("").trim();
            getVersion = getVersion.trim();
            Matcher n = p.matcher(getVersion);
            getVersion = n.replaceAll("").trim();
            if (Integer.parseInt(version) < Integer.parseInt(getVersion)) {
                return true;
            } else {
                return false;
            }
        }
    }

    public static int getVersionNumber(String version) {
        // 以 1.2.2为标准
        if (version != null && !"".equalsIgnoreCase(version)) {// 不为空或者null
            version = version.trim();
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(version);
            version = m.replaceAll("").trim();
            int versionInt = Integer.parseInt(version);
            return versionInt;
        }
        return 0;
    }
}
